<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js"></script>
<body>
<?php
/**
 * Created by PhpStorm.
 * User: extraxue
 * Date: 2018/5/2
 * Time: 下午10:43
 */

//    笔记：别的$_FILES类型还有：
//    echo "Upload: " . $_FILES["upFile"]["name"] . "<br />";
//    echo "Type: " . $_FILES["upFile"]["type"] . "<br />";
//    echo "Size: " . ($_FILES["upFile"]["size"] / 1024) . " Kb<br />";
//    echo "Stored in: " . $_FILES["upFile"]["tmp_name"];

header('Content-type:text/html;charset=utf8');
include("pdo.php");
require_once '../PHPExcel-1.8/Classes/PHPExcel.php';
require_once '../PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
require_once '../PHPExcel-1.8/Classes/PHPExcel/Reader/Excel5.php';

$randPwd = MD5(rand(10, 100));

if ((($_FILES["upFile"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") ||
        ($_FILES["upFile"]["type"] == "application/octet-stream"))
    && ($_FILES["upFile"]["size"] < 8388608)) {
    if ($_FILES["upFile"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
        $objReader = PHPExcel_IOFactory::createReader('excel2007'); //use Excel5 for 2003 format
    }
    if ($_FILES["upFile"]["type"] == "application/octet-stream") {
        $objReader = PHPExcel_IOFactory::createReader('Excel5');
    }
    $excelpath = $_FILES["upFile"]["tmp_name"];
    $objPHPExcel = $objReader->load($excelpath);
    $sheet = $objPHPExcel->getSheet(0);
    $highestRow = $sheet->getHighestRow();           //取得总行数
    $highestColumn = $sheet->getHighestColumn(); //取得总列数
    try {
        for ($j = 2; $j <= $highestRow; $j++)                        //从第二行开始读取数据
        {
            $str = "";
            for ($k = 'A'; $k <= $highestColumn; $k++)            //从A列读取数据
            {
                $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '|*|';//读取单元格
            }
            //去空格
            $str = trim(mb_convert_encoding($str, 'UTF-8', 'auto'));//根据自己编码修改
            $strs = explode("|*|", $str);
            $sqlChecUserkIsHave = "select * from user_info a where a.username='{$str[0]}' or a.number='{$strs[1]}'";
            $rsChecUserkIsHave = $pdo->query($sqlChecUserkIsHave);
            $resultChecUserkIsHave = $rsChecUserkIsHave->fetch(PDO::FETCH_ASSOC);
            if (!$resultChecUserkIsHave) {
                $sql = "insert into user_info (username,number,password,dept_id,status) values ('{$strs[0]}','{$strs[1]}','{$randPwd}','{$strs[2]}','2')";
                $pdo->exec($sql);
                ?>
                <div class="alert alert-green" style="margin-top: 10px;"><span
                            class=" "></span>添加&nbsp;<strong><?php echo "$strs[0],$strs[1]" ?></strong>&nbsp;成功。
                </div>
                <?php
            } else {
                ?>
                <div class="alert alert-red" style="margin-top: 10px;">
                    <span class=""></span>添加&nbsp;<strong><?php echo "$strs[0],$strs[1]" ?></strong>&nbsp;失败,请检查是否已经存在该用户名或学号&工号
                </div>
                <?php
            }
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
} else {
    echo '<script>alert("上传文件错误，请上传.xlsx或者.xls格式的小于8MB的excel文件");location.href="relDeptUser.php";</script>';
}
?>
</body>
</html>

